﻿    <?
require_once("inc.init.php");

$indexID = "userID";
$table   = "users";

if($_GET['action']=='delete')
{
    $messenger = new Messenger();

    $myUser = new AdMinUser();
    $myUser->setDBLink($dbLink);
    $_GET[$indexID]? $myUser->get((int)$_GET[$indexID]) : null;
    if ($myUser->delete())
    {
        $messenger->setMessageType(1);
        $messenger->addMessage("User successfully removed");
    }
    else $messenger->addMessage("Failed to delete: ". $messenger->getErrorMessage());
}

// SEARCH vars ans HTML
$searchFields = array($indexID, "username"); // inc.search.php explains this
$order = "userID ASC";
include DIR_ADMIN."/common/inc.search.php"; // inc.search.php sets several variables. See "variables setted"
$columnUrl = "index.php?". $columnUrl;

// MAIN QUERY
$sql = "SELECT * FROM ". $table ." WHERE ". $condition . $order ." LIMIT ". $position .", ". $module->numMaxListedRows;
$res = mysql_query($sql, $dbLink);

?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>AdMin | <?= $GLOBALS["website"]["name"] ?></title>
    <meta name="description" content="mobile first, app, web app, responsive, admin dashboard, flat, flat ui">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"> 
    <link rel="stylesheet" href="<?= $GLOBALS['CONF']['url']['admin']?>/css/bootstrap.css">
    <link rel="stylesheet" href="<?= $GLOBALS['CONF']['url']['admin']?>/css/font-awesome.min.css">
    <link rel="stylesheet" href="<?= $GLOBALS['CONF']['url']['admin']?>/css/plugin.css">
    <link rel="stylesheet" href="<?= $GLOBALS['CONF']['url']['admin']?>/css/font.css" cache="false">
    <link rel="stylesheet" href="<?= $GLOBALS['CONF']['url']['admin']?>/css/style.css">
    <!--[if lt IE 9]>
    <script src="js/ie/respond.min.js" cache="false"></script>
    <script src="js/ie/html5.js" cache="false"></script>
    <![endif]-->
<script src="<?= $GLOBALS["CONF"]["url"]["js"] ?>/lib/scriptaculous/scriptaculous.js?load=effects,controls,dragdrop" type="text/javascript" language="javascript" charset="utf-8"></script>
<script type='text/javascript' src='<?= $GLOBALS["CONF"]["url"]["js"] ?>/lib/litbox.js'></script>
<script type="text/javascript">
    function deletionConfirmDialog(id)
    {
        if (confirm('Are you sure you want to delete the content?')) {
                location.replace("?action=delete&<?= $indexID ?>="+id);
        } else {
            // Do nothing!
        }
    };
</script>
    <style>
    .left{
        float:left;
    }
    .right{
        float:right;
    }
    .end{
        clear:both;
    }
    </style>
</head>
<body>
    <section id="container">
        <?
        if($messenger)
        {
            $messages = $messenger->getArMessages();
            if(count($messages))
            {
                $html='<div id="respuesta_estado_orden" class="hidden alert fade in"><div class="left">';
                foreach($message as $key => $value)
                {
                    $html.=$value."<br>";
                }
                $html.='</div>
                            <div class="right">
                                <button class="btn_close_messeger close" data-dismiss="alert" type="button">×</button>
                            </div>
                            <div class="end"></div>
                        </div>';
            }
        }
        ?>
        <div class="row"><br>
            <div class="col-lg-12">
<section class="panel">
            <header class="panel-heading"><b><a href="index.php"><?= $module->name ?></a></b></header>
            <div class="panel-body">
              <div class="row text-small">
                <div class="col-sm-4 m-b-mini pull-left">
                    <a class="btn btn-sm btn-info" href="abm.php"><i class="icon-plus"></i> User</a>
                </div>
                <div class="col-sm-4 pull-right">
                    <form method="get" action="index.php" name="form1">
                        <div id="SearchFormBox">
                            <div class="input-group">
                                <input type="text" class="input-sm form-control" placeholder="Search" name="search" value="<?=$_GET['search']?>">
                                <span class="input-group-btn">
                                    <button class="btn btn-sm btn-white" type="submit">Go!</button>
                                </span>&nbsp;
                                <a href="index.php">[Reset]</a>
                            </div>
                        </div>
                    </form>
                </div>
              </div>
            </div>
            <div class="table-responsive">
              <table class="table table-striped b-t text-small">
                <thead>
                  <tr>
                    <th class="th-sortable" data-toggle="class">
                        <a href="<?= $columnUrl . $indexID ?>"><?= $indexID ?>
                            <span class="th-sort">
                                <?
                                if($_GET['order']=='userID')
                                {
                                    ?>
                                    <i class="icon-sort-down text<?=($_GET['order_type'] == 'ASC'?'-active':'')?>"></i>
                                    <i class="icon-sort-up text<?=($_GET['order_type'] == 'DESC'?'-active':'')?>"></i>
                                    <?
                                }
                                ?>
                                <i class="icon-sort"></i>
                            </span>
                        </a>
                    </th>
                    <th class="th-sortable" data-toggle="class">
                        <a href="<?= $columnUrl . 'username' ?>">Username
                            <span class="th-sort">
                                <?
                                if($_GET['order']=='username')
                                {
                                    ?>
                                        <i class="icon-sort-down text<?=($_GET['order_type'] == 'ASC'?'-active':'')?>"></i>
                                        <i class="icon-sort-up text<?=($_GET['order_type'] == 'DESC'?'-active':'')?>"></i>
                                    <?
                                }
                                ?>
                                <i class="icon-sort"></i>
                            </span>
                        </a>
                    </th>
                    <th class="th-sortable" data-toggle="class">
                        <a href="<?= $columnUrl . 'active' ?>">Active
                            <span class="th-sort">
                                <?
                                if($_GET['order']=='active')
                                {
                                    ?>
                                        <i class="icon-sort-down text<?=($_GET['order_type'] == 'ASC'?'-active':'')?>"></i>
                                        <i class="icon-sort-up text<?=($_GET['order_type'] == 'DESC'?'-active':'')?>"></i>
                                    <?
                                }
                                ?>
                                <i class="icon-sort"></i>
                            </span>
                        </a>
                    </th>
                    <th class="th-sortable" data-toggle="class">
                        <a href="<?= $columnUrl . 'date_created' ?>">Date Created
                            <span class="th-sort">
                                <?
                                if($_GET['order']=='date_created')
                                {
                                    ?>
                                        <i class="icon-sort-down text<?=($_GET['order_type'] == 'ASC'?'-active':'')?>"></i>
                                        <i class="icon-sort-up text<?=($_GET['order_type'] == 'DESC'?'-active':'')?>"></i>
                                    <?
                                }
                                ?>
                                <i class="icon-sort"></i>
                            </span>
                        </a>
                    </th>
                    <th width="120"></th>
                  </tr>
                </thead>
                <tbody>
                    <?
                    while($row = mysql_fetch_array($res))
                    {
                        $onClick = "document.location='abm.php?". $indexID ."=". $row[$indexID] ."';return false";
                        ?>
                        <tr>
                            <td><?=$row[$indexID]?></td>
                            <td><?=$row['name']?></td>
                            <td>
                            <a href="#" class="active" data-toggle="class"><i class="<?=($row['active']=='Y'?'icon-ok text-success':'icon-remove text-danger')?> icon-large text-active"></i></a>
                            </td>
                            <td><?= $row['date_created']?></td>
                            <td>
                                <a class="btn btn-white" title="Editar" href="abm.php?<?= $indexID ."=". $row[$indexID]?>"><i class="icon-pencil"></i></a>
                                <a class="btn btn-danger" title="Eliminar" href="javascript:deletionConfirmDialog(<?=$row[$indexID]?>)"><i class="icon-trash"></i></a>
                            </td>
                        </tr>
                        <?
                    }
                    ?>
                </tbody>
              </table>
            </div>
            <footer class="panel-footer">
              <div class="row">
                <div class="col-sm-5 text-right text-center-sm pull-right"><? showListPagination($urlVars, $numTotalRows); ?></div>
                </div>
              </div>
            </footer>
        <form action="index.php?<?= $moduleVars ?>" method="post" name="orderForm" id="orderForm" class="AbmTable" style="text-align:right; margin: 10px 0px; display:none">
            <span style="position: relative; top: -6px; margin-right: 10px">El orden de los contenidos ha cambiado!</span>
            <span class="button"><input type="submit" value="Guardar" name="Guardar"></span>
            <span class="button"><input type="submit" value="Cancelar" name="Cancelar" onClick="href.location='index.php?<?= $moduleVars ?>'"></span>
            <input name="newOrder" type="hidden" id="newOrder">
            <input name="action" type="hidden" value="orderContent">
        </form>
        <!-- Sortable table -->
        <script type="text/javascript">
            Sortable.create("mySortable", {
                handle:'grip',
                //ghosting:true,
                constraint:false,
                onUpdate:function(sortable) {
                    var newOrder = Sortable.serialize(sortable);
                    if (originalOrder!=newOrder) setTimeout('appearOrderForm()', 200);
                    else setTimeout('fadeOrderForm()', 200);
                    $('newOrder').value=newOrder;
                }
                // , onChange:function(element){$('debug').innerHTML = Sortable.serialize(element.parentNode) }
            });
            var originalOrder = Sortable.serialize("mySortable");
            function appearOrderForm()
            {
                Effect.Appear($('orderForm'));
            }
            function fadeOrderForm()
            {
                Effect.Fade($('orderForm'));
            }
        </script>
    </section>
    <script src="<?=$GLOBALS['CONF']['url']['admin']?>/js/jquery.min.js"></script>
    <!-- Bootstrap -->
    <script src="<?=$GLOBALS['CONF']['url']['admin']?>/js/bootstrap.js"></script>
    <!-- fuelux -->
    <script src="<?=$GLOBALS['CONF']['url']['admin']?>/js/fuelux/fuelux.js"></script>
    <script src="<?=$GLOBALS['CONF']['url']['admin']?>/js/underscore-min.js"></script>
    <!-- datatables -->
    <script src="<?=$GLOBALS['CONF']['url']['admin']?>/js/datatables/jquery.dataTables.min.js"></script>
</body>
</html>
